<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Demand extends CI_Controller {

	function __construct() {
		parent::__construct();

		
		$this->load->model('client_model');
		$this->load->model('demand_model');
		$this -> load -> model('object_usage_model');
		$this -> load -> model('required_level_model');
		$this -> load -> model('customer_source_model');
		$this -> load -> model('address_city_model');
		$this -> load -> model('address_county_model');
		$this -> load -> model('emphasis_area_model');
		$this -> load -> model('building_name_model');
		$this -> load -> model('object_model');
		$this -> load -> model('users_model');
		
		$this -> load -> model('client/contact', 'contact');
	}

	public function qwe() {

		$user_purview = check_broker_is_visible();
		echo $this -> login_lib -> users_id();
	}

	public function get_demand_field() {

		$q = $this -> demand_model -> get_field();

		$field_ar = array();

		foreach ($q as $field) {
			$field_ar[] = $field;
		}

		return $field_ar;

	}

	public function data_entry() {

		$html = $this -> init("需求資料輸入", "jquery,demand,demand_ready,park,school,map,address", "form");
		$this -> load -> model('object_usage_model');
		$html["object_usage"] = $this -> object_usage_model -> get_all();
		$this -> load -> model('mrt_line_model');
		$html["mrt_line"] = $this -> mrt_line_model -> get_all();
		$this -> load -> model('windows_model');
		$html["windows"] = $this -> windows_model -> get_all();
		$this -> load -> model('base_is_located_model');
		$html["base_is_located"] = $this -> base_is_located_model -> get_all();
		$this -> load -> model('object_styles_model');
		$html["object_styles"] = $this -> object_styles_model -> get_all();
		$this -> load -> model('public_facilities_model');
		$html["public_facilities"] = $this -> public_facilities_model -> get_all();
		$this -> load -> model('affiliated_with_model');
		$html["affiliated_with"] = $this -> affiliated_with_model -> get_all();
		$this -> load -> model('management_model');
		$html["management"] = $this -> management_model -> get_all();
		$this -> load -> model('partition_the_model');
		$html["partition_the"] = $this -> partition_the_model -> get_all();
		$this -> load -> model('land_use_zoning_model');
		$html["land_use_zoning"] = $this -> land_use_zoning_model -> get_all();
		$this -> load -> model('customer_source_model');
		$html["customer_source"] = $this -> customer_source_model -> get_all();
		$this -> load -> model('required_level_model');
		$html["required_level"] = $this -> required_level_model -> get_all();
		$this -> load -> model('currency_model');
		$html["currency"] = $this -> currency_model -> get_all();
		$this -> load -> model('area_unit_model');
		$html["area_unit"] = $this -> area_unit_model -> get_all();
		$this -> load -> model('address_city_model');
		$html["address_city"] = $this -> address_city_model -> get_all();
		$this -> load -> model('address_county_model');
		$html["address_county"] = $this -> address_county_model -> get_by_id('1');
		$this -> load -> model('school_class_model');
		$html["school_class"] = $this -> school_class_model -> get_all();
		$this -> load -> model('emphasis_area_model');
		$html["emphasis_area"] = $this -> emphasis_area_model -> get_all();
		$this -> load -> model('building_name_model');
		$html["building_name"] = $this -> building_name_model -> get_all();
		$this -> load -> model('object_model');
		$html["object_case_name"] = $this -> object_model -> get_all_object();

		//print_r($html["object_case_name"]);

		$this -> load -> model('circle_model');
		$this -> load -> model('users_model');

		$html["users"] = $this -> users_model -> get_all_users();

		$html["circle"] = $this -> circle_model -> get_all();
		$this -> load -> view('demand/demand_data_entry', $html);
	}

	

	public function check_buidling_name($field_name, $value) {
		//簡查大樓名稱
		$this -> load -> model('check_model');
		$this -> check_model -> set_table_name("h_building_name");
		$this -> check_model -> set_key_name("building_name_id");
		$_id = $this -> check_model -> check_building_name_exist($field_name, $value);
		return $_id;
	}

	public function data_entry_submit_add() {

		
		$data['type_rent'] = $this -> input -> post('type_rent');
		$data['type_sell'] = $this -> input -> post('type_sell');
		$data['type_consignment'] = $this -> input -> post('type_consignment');
		$data['object_usage'] = $this -> input -> post('object_usage');
		$data['name'] = $this -> input -> post('name');
		$data['mobile'] = $this -> input -> post('mobile');
		$data['home_phone'] = $this -> input -> post('home_phone');
		$data['company_phone'] = $this -> input -> post('company_phone');
		$data['home_address'] = $this -> input -> post('home_address');
		
		$data['demand_serial'] = $this -> input -> post('demand_serial');

		$data['building_name'] = $this -> input -> post('building_name');

		if ($this -> input -> post('building_name') != "") {

			$re_id = $this -> check_buidling_name("value", $data['building_name']);
			$data['building_name'] = $re_id;
			//$this->input->post('home_building_name');
		}

		$data['company_address'] = $this -> input -> post('company_address');
		$data['company_name'] = $this -> input -> post('company_name');
		$data['email'] = $this -> input -> post('email');
		$data['contact'] = $this -> input -> post('contact');
		$data['contact_phone'] = $this -> input -> post('contact_phone');
		$data['building_area1'] = $this -> input -> post('building_area1');
		$data['building_area2'] = $this -> input -> post('building_area2');
		$data['parking_number'] = $this -> input -> post('parking_number');
		$data['acceptable_mechanical'] = $this -> input -> post('acceptable_mechanical');
		$data['indoor_area1'] = $this -> input -> post('indoor_area1');
		$data['indoor_area2'] = $this -> input -> post('indoor_area2');
		$data['land_area1'] = $this -> input -> post('land_area1');
		$data['land_area2'] = $this -> input -> post('land_area2');
		$data['key_areas1'] = $this -> input -> post('key_areas1');
		$data['key_areas2'] = $this -> input -> post('key_areas2');
		$data['specify_building1'] = $this -> input -> post('specify_building1');
		$data['specify_building2'] = $this -> input -> post('specify_building2');
		$data['sections1'] = $this -> input -> post('sections1');
		$data['sections2'] = $this -> input -> post('sections2');
		$data['object_name1'] = $this -> input -> post('object_name1');
		$data['object_name2'] = $this -> input -> post('object_name2');
		$data['mrt_line1'] = $this -> input -> post('mrt_line1');
		$data['mrt_start1'] = $this -> input -> post('mrt_start1');
		$data['mrt_until1'] = $this -> input -> post('mrt_until1');
		$data['mrt_line2'] = $this -> input -> post('mrt_line2');
		$data['mrt_start2'] = $this -> input -> post('mrt_start2');
		$data['mrt_until2'] = $this -> input -> post('mrt_until2');
		$data['range_east'] = $this -> input -> post('range_east');
		$data['range_north'] = $this -> input -> post('range_north');
		$data['range_west'] = $this -> input -> post('range_west');
		$data['range_south'] = $this -> input -> post('range_south');
		$data['park1'] = $this -> input -> post('park1');
		$data['park2'] = $this -> input -> post('park2');
		$data['school1'] = $this -> input -> post('school1');
		$data['school2'] = $this -> input -> post('school2');
		$data['circle1'] = $this -> input -> post('circle1');
		$data['circle2'] = $this -> input -> post('circle2');
		$data['administrative_area1'] = $this -> input -> post('administrative_area1');
		$data['administrative_area2'] = $this -> input -> post('administrative_area2');
		$data['total_budget1'] = $this -> input -> post('total_budget1');
		$data['total_budget2'] = $this -> input -> post('total_budget2');
		$data['budget_per_ping1'] = $this -> input -> post('budget_per_ping1');
		$data['budget_per_ping2'] = $this -> input -> post('budget_per_ping2');
		$data['reported_rate1'] = $this -> input -> post('reported_rate1');
		$data['reported_rate2'] = $this -> input -> post('reported_rate2');
		$data['leasing_business_purposes'] = $this -> input -> post('leasing_business_purposes');
		$data['year_of_construction'] = $this -> input -> post('year_of_construction');
		//$data['object_case_type'] = join("object_type",$this->input->post('object_case_type'));

		$object_case_type_flag = true;
		if ($this -> input -> post('object_case_type')) {

			$ar_object_case_types = $this -> input -> post('object_case_type');
			$case_type = "";

			foreach ($ar_object_case_types as $num => $value) {

				if ($object_case_type_flag) {
					$object_case_type_flag = false;
					$case_type .= $value;
				} else {
					$case_type .= "," . $value;
				}
			}

			$data['object_case_type'] = $case_type;
			//$this->input->post('object_case_type');
		}

		$data['windows'] = $this -> input -> post('windows');
		$data['facing_the_household'] = $this -> input -> post('facing_the_household');
		$data['building_facing'] = $this -> input -> post('building_facing');
		$data['designated_floors1'] = $this -> input -> post('designated_floors1');
		$data['designated_floors2'] = $this -> input -> post('designated_floors2');
		$data['floors'] = $this -> input -> post('floors');
		$data['object_styles'] = $this -> input -> post('object_styles');
		$data['public_facilities'] = $this -> input -> post('public_facilities');
		$data['accessories_with'] = $this -> input -> post('accessories_with');
		$data['management'] = $this -> input -> post('management');
		$data['number_of_rooms1'] = $this -> input -> post('number_of_rooms1');
		$data['number_of_rooms2'] = $this -> input -> post('number_of_rooms2');
		$data['number_of_hall1'] = $this -> input -> post('number_of_hall1');
		$data['number_of_hall2'] = $this -> input -> post('number_of_hall2');
		$data['number_of_toilet1'] = $this -> input -> post('number_of_toilet1');
		$data['number_of_toilet2'] = $this -> input -> post('number_of_toilet2');
		$data['number_of_suite1'] = $this -> input -> post('number_of_suite1');
		$data['number_of_suite2'] = $this -> input -> post('number_of_suite2');
		$data['partition_the'] = $this -> input -> post('partition_the');
		$data['floor_height'] = $this -> input -> post('floor_height');
		$data['customer_source'] = $this -> input -> post('customer_source');
		$data['required_level'] = $this -> input -> post('required_level');
		$data['remark'] = $this -> input -> post('remark');
		$data['currency'] = $this -> input -> post('currency');
		$data['area_unit'] = $this -> input -> post('area_unit');
		$data['remark'] = $this -> input -> post('remark');
		$data['status'] = $this -> input -> post('status');
		$data['invalid_description'] = $this -> input -> post('invalid_description');
		$data['front_man'] = $this -> input -> post('front_man');
		$data['head'] = $this -> input -> post('head');
		$data['original_broker'] = $this -> input -> post('original_broker');
		$data['broker'] = $this -> input -> post('broker');
		$analog_pairing_submit = $this -> input -> post('analog_pairing_submit');
		$data['demand_id'] = $this -> input -> post('demand_id');
		$data['client_id'] = $this -> input -> post('client_id');
		$data['ref_client_verify_code'] = $this -> input -> post('ref_client_verify_code');
		$data['client_contact_id'] = $this -> input -> post('client_contact_id');
		
		$data['sercet_level'] = $this -> input -> post('sercet_level');
		$data['is_public'] = $this -> input -> post('is_public');
		$data['important'] = $this -> input -> post('important');
		$data['is_see'] = $this -> input -> post('is_see');
		$data['ctime'] = time();
		$data['mtime'] = time();		

		$ref_client_verify_code = $data['ref_client_verify_code'];

		//print_r($data);

		$verify_code = MD5(date("Ymdhis"));
		$client = $this -> input -> post('client');
		

		if ($ref_client_verify_code == "false") {

			$verify_code = MD5(date("Ymdhis"));

			$this -> load -> model("users_model");
			$this -> load -> library('session');
			$c_ss = $this -> session -> all_userdata();
			$_head_info = $this -> users_model -> get_user_by_user('劉陳傳');
			//接待人   ,   經紀人
			$client["front_man"] = $c_ss["users_id"];
			$client["broker"] = $c_ss["users_id"];
			//主管
			$client["head"] = $_head_info -> row() -> users_id;

			//增加顧客';
			$client['ref_client_verify_code'] = $verify_code;

			//print_r($client);

			$this -> client_model -> add_client2($client);
			
			
			
			$contact_data = array("name" => $_POST["contact_name"], 
								"home_phone" => $_POST["contact_home_phone"], 
								"ref_client_verify_code" => $verify_code);
			//增加聯絡人
			$this -> client_model -> demand_insert_contact($contact_data);

			//去資料庫找然後設定
			$contact_info = $this -> contact -> get_contact_by_name_verify($verify_code, $_POST["contact_name"]);
			$client_info = $this -> client_model -> get_client_by_verify_code($verify_code);

			//設定demand 的資料
			$data['client_contact_id'] = $contact_info -> row() -> client_contact_id;
			$data['client_id'] = $client_info -> row() -> client_id;
			$data['ref_client_verify_code'] = $verify_code;
			//增加需求
			$this -> demand_model -> add_demand_by_victor($data);

		} else {

			//echo 'sql 寫回資料庫';
			$client_result = $this -> client_model -> get_client_by_name($client["name"]);
			$data['ref_client_verify_code'] = $ref_client_verify_code;

			$client_info = $client_result -> row();

			//設定demand 寫入資料庫
			$data["client_id"] = $client_info -> client_id;

			//if ($this -> input -> post('client_contact_id') != "false") {

				$data['client_contact_id'] = $this -> input -> post('client_contact_id');

			//} else {

				$contact_data = array(
						"client_id" => $client_info -> client_id,
					    "name" => $_POST["contact_name"], 
						"home_phone" => $_POST["contact_home_phone"], 
						"ref_client_verify_code" => $ref_client_verify_code);
				//增加聯絡人
				$this -> client_model -> demand_insert_contact($contact_data);

				//去資料庫找然後設定
				$contact_info = $this -> contact -> get_contact_by_name_verify($ref_client_verify_code, 
					$_POST["contact_name"]);

				//設定demand 的資料
				$data['client_contact_id'] = $contact_info -> row() -> client_contact_id;
			//}
			//增加需求
			$this -> demand_model -> add_demand_by_victor($data);
		}
		//print_r($client);
		redirect("/demand/person_query");
	}


	//輸入完之後要再入的頁面
	public function asdzxcqwe() {
		$html = $this -> init("個人查詢", "", "jquery");
		$this -> load -> view('demand/crid/demand_crid', $html);
				
	}
	
	//
	public function person_query() {
		
		 
		$html = $this -> init("需求－個人查詢", "jquery,demand,date,address,object", "form");
				
		$html["required_level"] = $this -> required_level_model -> get_all();		
		$html["object_usage"] = $this -> object_usage_model -> get_all();		
		$html["customer_source"] = $this -> customer_source_model -> get_all();		
		$html["address_city"] = $this -> address_city_model -> get_all();		
		$html["address_county"] = $this -> address_county_model -> get_by_id('1');		
		$html["emphasis_area"] = $this -> emphasis_area_model -> get_all();		
		$html["building_name"] = $this -> building_name_model -> get_all();		
		$html["object_case_name"] = $this -> object_model -> get_object_case_name();
		$html["all_users"] = $this -> users_model -> get_all_users();
		$html["post_data"]=$_POST;
		$html["query_html"]=$this -> load -> view('demand/demand_data_query2' ,$html,true);
		//$this -> load -> view('demand/demand_data_query', $html);
		 	
		$this -> load -> view('demand/crid/person_crid', $html);
		
				
	}

	public function company_query() {
		
		 
		$html = $this -> init("需求－公司查詢", "jquery,demand,date,address,object", "form");
				
		$html["required_level"] = $this -> required_level_model -> get_all();		
		$html["object_usage"] = $this -> object_usage_model -> get_all();		
		$html["customer_source"] = $this -> customer_source_model -> get_all();		
		$html["address_city"] = $this -> address_city_model -> get_all();		
		$html["address_county"] = $this -> address_county_model -> get_by_id('1');		
		$html["emphasis_area"] = $this -> emphasis_area_model -> get_all();		
		$html["building_name"] = $this -> building_name_model -> get_all();		
		$html["object_case_name"] = $this -> object_model -> get_object_case_name();
		$html["all_users"] = $this -> users_model -> get_all_users();
		$html["post_data"]=$_POST;
		$html["query_html"]=$this -> load -> view('demand/demand_data_query2' ,$html,true);
		//$this -> load -> view('demand/demand_data_query', $html);
		 	
		$this -> load -> view('demand/crid/company_crid', $html);
		
				
	}

	public function data_query() {
		
		$type = '';
		$html["type"] = '';

		if ($_GET) {
			$type = $_GET["type"];
			$html["type"] = $type;
		}

		$html = $this -> init("需求资料查询", "jquery,demand,demand_data_query_ready,date,address", "form");
				
		$html["required_level"] = $this -> required_level_model -> get_all();		
		$html["object_usage"] = $this -> object_usage_model -> get_all();		
		$html["customer_source"] = $this -> customer_source_model -> get_all();		
		$html["address_city"] = $this -> address_city_model -> get_all();		
		$html["address_county"] = $this -> address_county_model -> get_by_id('1');		
		$html["emphasis_area"] = $this -> emphasis_area_model -> get_all();		
		$html["building_name"] = $this -> building_name_model -> get_all();		
		$html["object_case_name"] = $this -> object_model -> get_object_case_name();
		$html["all_users"] = $this -> users_model -> get_all_users();
		$html["type"] = $_GET["type"];
		$this -> load -> view('demand/demand_data_query', $html);
		
	}

	
	 public function demand_get_client_name() {
	 	
	 	
		
	 	$cs=$this->client_model->get_all();
		$ds=$this->demand_model->get_all();
		
		
		$client_ar=array();
		foreach($cs as $c=>$s){
			$client_ar[$s->client_id]=$s->name;
		}
		
		foreach($ds as $d=>$s){
			$data=array('name'=> $client_ar[$s->client_id] );
			//echo $s->demand_id;
			//echo $s->client_id;
			$this->demand_model->update($s->demand_id,$data);
		}
		
	 	
	 }

	//查詢person
	public function the_data_query_results() {
		
		
		
		
		
		$type_title = '';
		if (!empty($_POST["type"])) {

			if ($_POST["type"] == 'person') {
				$type_title = '個人';
			}

			if ($_POST["type"] == 'company') {
				$type_title = '公司';
			}

		}
		//echo $type_title = '公司';
		//print_r($_POST);
		if( empty($_POST)){
			$type_title = '個人';
		}
		$html = $this -> init("需求-" . $type_title . "-資料查詢結果", "", "");
		$html["post_data"] = $_POST;
		
		if( empty($_POST)){
			$this -> load -> view('demand/crid/demand_crid', $html);
		}
		
		if($_POST["type"]=='company'){
			$this -> load -> view('demand/crid/company_demand_crid', $html);
		}
		
		if($_POST["type"]=='person'){
			$this -> load -> view('demand/crid/demand_crid', $html);
		}
		
		
	}
	
	

	//查詢company
	public function company_data_query_results($sequence = null) {

		$html = $this -> init("需求-公司-資料查結果", "jquery,demand,pagination", "");
		$html["post_data"] = $_POST;
		$this -> load -> view('demand/crid/company_crid', $html);
	}

	//查詢
	public function data_query_results($sequence = null) {
		//$html["title"] ='需求资料查询结果';
		$html = $this -> init("需求资料查询结果", "jquery,demand,pagination", "");

		$html["post_data"] = $_POST;
		$this -> load -> view('demand/crid/demand_crid', $html);
	}

	//查詢
	public function data_query_results_crid($sequence = null) {

		$html = $this -> init("需求资料查询结果", "jquery,demand,pagination", "table");
		$this -> load -> model('demand_model');
		$keyword = $this -> input -> post('keywords');
		$where = array();

		if ($this -> input -> post('parking_number') != "")
			$where['parking_number'] = $this -> input -> post('parking_number');

		if ($this -> input -> post('name') != "")
			$where['name'] = $this -> input -> post('name');
		if ($this -> input -> post('required_level') != "")
			$where['required_level'] = $this -> input -> post('required_level');
		if ($this -> input -> post('type_sell') != "")
			$where['type_sell'] = $this -> input -> post('type_sell');
		if ($this -> input -> post('type_rent') != "")
			$where['type_rent'] = $this -> input -> post('type_rent');
		if ($this -> input -> post('type_consignment') != "")
			$where['type_consignment'] = $this -> input -> post('type_consignment');
		if ($this -> input -> post('object_usage') != "")
			$where['object_usage'] = $this -> input -> post('object_usage');
		if ($this -> input -> post('building_area1') != "")
			$where['building_area1'] = $this -> input -> post('building_area1');
		if ($this -> input -> post('building_area2') != "")
			$where['building_area2'] = $this -> input -> post('building_area2');
		if ($this -> input -> post('indoor_area1') != "")
			$where['indoor_area1'] = $this -> input -> post('indoor_area1');
		if ($this -> input -> post('indoor_area2') != "")
			$where['indoor_area2'] = $this -> input -> post('indoor_area2');
		if ($this -> input -> post('land_area1') != "")
			$where['land_area1'] = $this -> input -> post('land_area1');
		if ($this -> input -> post('land_area2') != "")
			$where['land_area2'] = $this -> input -> post('land_area2');
		if ($this -> input -> post('total_budget1') != "")
			$where['total_budget1'] = $this -> input -> post('total_budget1');
		if ($this -> input -> post('total_budget2') != "")
			$where['total_budget2'] = $this -> input -> post('total_budget2');
		if ($this -> input -> post('key_areas') != "")
			$where['key_areas'] = $this -> input -> post('key_areas');
		//fix by victor
		if ($this -> input -> post('key_areas1') != "")
			$where['key_areas1'] = $this -> input -> post('key_areas1');
		if ($this -> input -> post('key_areas2') != "")
			$where['key_areas2'] = $this -> input -> post('key_areas2');

		if ($this -> input -> post('specify_building1') != "")
			$where['specify_building1'] = $this -> input -> post('specify_building1');

		if ($this -> input -> post('specify_building2') != "")
			$where['specify_building2'] = $this -> input -> post('specify_building2');

		if ($this -> input -> post('specify_building') != "")
			$where['specify_building'] = $this -> input -> post('specify_building');

		//fix by victor

		if ($this -> input -> post('object_name') != "")
			$where['object_name'] = $this -> input -> post('object_name');

		if ($this -> input -> post('sections1') != "")
			$where['sections1'] = $this -> input -> post('sections1');
		if ($this -> input -> post('sections2') != "")
			$where['sections2'] = $this -> input -> post('sections2');
		if ($this -> input -> post('administrative_area') != "")
			$where['administrative_area'] = $this -> input -> post('administrative_area');
		if ($this -> input -> post('year_of_construction1') != "")
			$where['year_of_construction1'] = $this -> input -> post('year_of_construction1');
		if ($this -> input -> post('year_of_construction2') != "")
			$where['year_of_construction2'] = $this -> input -> post('year_of_construction2');

		//if($this->input->post('object_case_type')!="")
		//  $where['object_case_type'] = $this->input->post('object_case_type');
		if ($this -> input -> post('status') != "")
			$where['status'] = $this -> input -> post('status');
		if ($this -> input -> post('customer_source') != "")
			$where['customer_source'] = $this -> input -> post('customer_source');
		if ($this -> input -> post('sctime') != "")
			$where['sctime'] = $this -> input -> post('sctime');
		if ($this -> input -> post('ectime') != "")
			$where['ectime'] = $this -> input -> post('ectime');
		if ($this -> input -> post('broker') != "")
			$where['broker'] = $this -> input -> post('broker');
		$condition = "";
		foreach ($where as $key => $val) {
			if ($condition == "")
				$condition .= $key . "|" . $val;
			else
				$condition .= "---" . $key . "|" . $val;
		}
		if ($keyword != "")
			$html["query_condition"] = "keyword|" . $keyword . "%%" . $condition;
		else
			$html["query_condition"] = $condition;

		$config['per_page'] = 2;
		$config['total_rows'] = $this -> demand_model -> query_demand_count($where, $keyword);
		$config['url'] = '/demand/data_query_results_ajax';
		$config['num_pages'] = ceil($config['total_rows'] / $config['per_page']);
		$limit = $config['per_page'] . ",0";
		$html['page'] = $config;
		$html['sequence'] = "";

		/*         * *******對的*********** */
		$this -> load -> library('pagination');
		$config['base_url'] = '/demand/data_query_results';
		$config['total_rows'] = $this -> demand_model -> query_demand_count($where, $keyword);
		$config['per_page'] = 10;
		$limit = $config['per_page'] . "," . $sequence;
		$this -> pagination -> initialize($config);
		/*         * *******對的*********** */

		$html["victor_page"] = $this -> pagination -> create_links();

		//print_r($where);

		$demand_result = $this -> demand_model -> query_demand($where, $keyword, $limit);

		//print_r($demand_result

		$html["result"] = $demand_result;
		$this -> load -> model('required_level_model');
		$html["required_level"] = $this -> required_level_model -> get_required_level();
		$this -> load -> model('object_usage_model');
		$html["object_usage"] = $this -> object_usage_model -> get_object_usage();
		$this -> load -> view('crid/demand_crid', $html);
	}

	//配對
	public function data_query_results_match($sequence = null) {

		foreach ($_POST as $name => $vv) {

			if (!empty($vv)) {
				//echo $name.'=>'.$vv;
				//echo "<br>";
			}
		}

		$html = $this -> init("需求资料查询结果", "jquery,demand,pagination", "table");
		$this -> load -> model('demand_model');
		$keyword = $this -> input -> post('keywords');
		$where = array();

		//$data['parking_number'] = $this->input->post('parking_number');

		if ($this -> input -> post('parking_number') != "")
			$where['parking_number'] = $this -> input -> post('parking_number');

		if ($this -> input -> post('name') != "")
			$where['name'] = $this -> input -> post('name');
		if ($this -> input -> post('required_level') != "")
			$where['required_level'] = $this -> input -> post('required_level');
		if ($this -> input -> post('type_sell') != "")
			$where['type_sell'] = $this -> input -> post('type_sell');
		if ($this -> input -> post('type_rent') != "")
			$where['type_rent'] = $this -> input -> post('type_rent');
		if ($this -> input -> post('type_consignment') != "")
			$where['type_consignment'] = $this -> input -> post('type_consignment');
		if ($this -> input -> post('object_usage') != "")
			$where['object_usage'] = $this -> input -> post('object_usage');
		if ($this -> input -> post('building_area1') != "")
			$where['building_area1'] = $this -> input -> post('building_area1');
		if ($this -> input -> post('building_area2') != "")
			$where['building_area2'] = $this -> input -> post('building_area2');
		if ($this -> input -> post('indoor_area1') != "")
			$where['indoor_area1'] = $this -> input -> post('indoor_area1');
		if ($this -> input -> post('indoor_area2') != "")
			$where['indoor_area2'] = $this -> input -> post('indoor_area2');
		if ($this -> input -> post('land_area1') != "")
			$where['land_area1'] = $this -> input -> post('land_area1');
		if ($this -> input -> post('land_area2') != "")
			$where['land_area2'] = $this -> input -> post('land_area2');
		if ($this -> input -> post('total_budget1') != "")
			$where['total_budget1'] = $this -> input -> post('total_budget1');
		if ($this -> input -> post('total_budget2') != "")
			$where['total_budget2'] = $this -> input -> post('total_budget2');
		if ($this -> input -> post('key_areas') != "")
			$where['key_areas'] = $this -> input -> post('key_areas');
		//fix by victor
		if ($this -> input -> post('key_areas1') != "")
			$where['key_areas1'] = $this -> input -> post('key_areas1');
		if ($this -> input -> post('key_areas2') != "")
			$where['key_areas2'] = $this -> input -> post('key_areas2');

		if ($this -> input -> post('specify_building1') != "")
			$where['specify_building1'] = $this -> input -> post('specify_building1');

		if ($this -> input -> post('specify_building2') != "")
			$where['specify_building2'] = $this -> input -> post('specify_building2');

		if ($this -> input -> post('specify_building') != "")
			$where['specify_building'] = $this -> input -> post('specify_building');

		//fix by victor

		if ($this -> input -> post('object_name') != "")
			$where['object_name'] = $this -> input -> post('object_name');

		if ($this -> input -> post('sections1') != "")
			$where['sections1'] = $this -> input -> post('sections1');
		if ($this -> input -> post('sections2') != "")
			$where['sections2'] = $this -> input -> post('sections2');
		if ($this -> input -> post('administrative_area') != "")
			$where['administrative_area'] = $this -> input -> post('administrative_area');
		if ($this -> input -> post('year_of_construction1') != "")
			$where['year_of_construction1'] = $this -> input -> post('year_of_construction1');
		if ($this -> input -> post('year_of_construction2') != "")
			$where['year_of_construction2'] = $this -> input -> post('year_of_construction2');

		//if($this->input->post('object_case_type')!="")
		//	$where['object_case_type'] = $this->input->post('object_case_type');
		if ($this -> input -> post('status') != "")
			$where['status'] = $this -> input -> post('status');
		if ($this -> input -> post('customer_source') != "")
			$where['customer_source'] = $this -> input -> post('customer_source');
		if ($this -> input -> post('sctime') != "")
			$where['sctime'] = $this -> input -> post('sctime');
		if ($this -> input -> post('ectime') != "")
			$where['ectime'] = $this -> input -> post('ectime');
		if ($this -> input -> post('broker') != "")
			$where['broker'] = $this -> input -> post('broker');

		if ($this -> input -> post('circle1') != "")
			$where['circle1'] = $this -> input -> post('circle1');
		if ($this -> input -> post('circle2') != "")
			$where['circle2'] = $this -> input -> post('circle2');
		$condition = "";
		foreach ($where as $key => $val) {
			if ($condition == "")
				$condition .= $key . "|" . $val;
			else
				$condition .= "---" . $key . "|" . $val;
		}
		if ($keyword != "")
			$html["query_condition"] = "keyword|" . $keyword . "%%" . $condition;
		else
			$html["query_condition"] = $condition;
		$config['per_page'] = 15;
		$config['total_rows'] = $this -> demand_model -> query_demand_count($where, $keyword);
		$config['url'] = '/demand/data_query_results_ajax';
		$config['num_pages'] = ceil($config['total_rows'] / $config['per_page']);
		$limit = $config['per_page'] . ",0";
		$html['page'] = $config;
		$html['sequence'] = "";

		$resulta = $this -> demand_model -> match_demand($where);
		//$result->;
		$html["result"] = $resulta;

		$this -> load -> model('required_level_model');
		$html["required_level"] = $this -> required_level_model -> get_required_level();
		$this -> load -> model('object_usage_model');
		$html["object_usage"] = $this -> object_usage_model -> get_object_usage();
		$this -> load -> view('demand_data_query_match', $html);
	}

	public function data_query_results_ajax() {
		$this -> load -> model('demand_model');
		$config['per_page'] = 15;
		$query_condition = $this -> input -> post('query_condition');
		$page = $this -> input -> post('page');
		$sequence = $this -> input -> post('sequence');
		$where = $this -> resolve($query_condition, "where");
		$keyword = $this -> resolve($query_condition);
		$p = $config['per_page'] * ($page - 1);
		$limit = $config['per_page'] . "," . $p;
		$html["result"] = $this -> demand_model -> query_demand($where, $keyword, $limit);
		$this -> load -> model('required_level_model');
		$html["required_level"] = $this -> required_level_model -> get_all();
		$this -> load -> view('demand_data_query_results_ajax', $html);
	}

	public function resolve($query_condition, $type = "") {
		$keyword = "";
		$where = array();
		$e = explode('%%', $query_condition);
		if (count($e) > 1) {
			$e0 = explode('|', $e[0]);
			$keyword = $e0[1];
			$e1 = explode('---', $e[1]);
			if (count($e1) > 1) {
				foreach ($e1 as $key => $val) {
					$e2 = explode('|', $val);
					$where[$e2[0]] = $e2[1];
				}
			} else {
				$e2 = explode('|', $val);
				$where[$e2[0]] = $e2[1];
			}
		} else {
			$e1 = explode('---', $e[0]);
			if (count($e1) > 1) {
				foreach ($e1 as $key => $val) {
					$e2 = explode('|', $val);
					$where[$e2[0]] = $e2[1];
				}
			} else {
				if ($e[0] != "") {
					$e2 = explode('|', $e[0]);
					$where[$e2[0]] = $e2[1];
				}
			}
		}
		if ($type == "where")
			return $where;
		else
			return $keyword;
	}

	public function data_edit($id = null) {

		if (empty($id)) {
			$id = $_GET["id"];
		}

		$html["show_contact"] = 0;

		$html = $this -> init("需求資料修改", "jquery,demand,demand_ready,park,school,address", "form");
		$this -> load -> model('object_usage_model');
		$html["object_usage"] = $this -> object_usage_model -> get_all();
		$this -> load -> model('mrt_line_model');
		$html["mrt_line"] = $this -> mrt_line_model -> get_all();

		$this -> load -> model('building_name_model');
		$html["buildings"] = $this -> building_name_model -> get_all();

		$this -> load -> model('windows_model');
		$html["windows"] = $this -> windows_model -> get_all();
		$this -> load -> model('base_is_located_model');
		$html["base_is_located"] = $this -> base_is_located_model -> get_all();
		$this -> load -> model('object_styles_model');
		$html["object_styles"] = $this -> object_styles_model -> get_all();
		$this -> load -> model('public_facilities_model');
		$html["public_facilities"] = $this -> public_facilities_model -> get_all();
		$this -> load -> model('affiliated_with_model');
		$html["affiliated_with"] = $this -> affiliated_with_model -> get_all();
		$this -> load -> model('management_model');
		$html["management"] = $this -> management_model -> get_all();
		$this -> load -> model('land_use_zoning_model');
		$html["land_use_zoning"] = $this -> land_use_zoning_model -> get_all();
		$this -> load -> model('customer_source_model');
		$html["customer_source"] = $this -> customer_source_model -> get_all();
		$this -> load -> model('required_level_model');
		$html["required_level"] = $this -> required_level_model -> get_all();
		$this -> load -> model('currency_model');
		$html["currency"] = $this -> currency_model -> get_all();
		$this -> load -> model('area_unit_model');
		$html["area_unit"] = $this -> area_unit_model -> get_all();
		$this -> load -> model('address_city_model');
		$html["address_city"] = $this -> address_city_model -> get_all();
		$this -> load -> model('address_county_model');
		$html["address_county"] = $this -> address_county_model -> get_by_id('1');
		$this -> load -> model('demand_model');
		$html["demand"] = $this -> demand_model -> get_demand_info($id);
		$this -> load -> model('mrt_model');
		$html["mrt"] = $this -> mrt_model -> get_mrt_by_mrt_line();
		$this -> load -> model('school_class_model');
		$html["school_class"] = $this -> school_class_model -> get_all();
		$this -> load -> model('emphasis_area_model');
		$html["emphasis_area"] = $this -> emphasis_area_model -> get_all();
		$this -> load -> model('building_name_model');
		$html["building_name"] = $this -> building_name_model -> get_all();

		$this -> load -> model('object_model');
		$html["object_case_name"] = $this -> object_model -> get_all_object();
		//$html["object_case_name"] = $this -> object_model -> get_object_case_name();

		$this -> load -> model('circle_model');
		$this -> load -> model('client_model');
		$this -> load -> model('client/contact', 'contact');

		//$html["demand"]->demand_id;

		$contact_info  = $this -> contact -> get_contact_by_id($html["demand"] -> client_contact_id);
		$client_info   = $this -> client_model -> get_client_info($html["demand"] -> client_id);

		//print_r($html["demand"] -> client_id);
		//print_r( $contact_info);
		//echo count($contact_info);
		//echo [$contact_info]0]->name;
		if (!empty($client_info))
			$html["client_info"] = $client_info;

		if (count($contact_info) > 0) {
			$html["contact_info"] = $contact_info;
		} else {
			$html["contact_info"] = '';
		}

		//print_r($client_info );
		//$html["contacts"] = $this->client_model->get_object_case_name();

		$this -> load -> model('client/contact', 'contact');
		$this -> load -> model('client/relationship', 'relationship');

		$verify_code = $html["demand"] -> ref_client_verify_code;

		$html["contacts"] = $this -> contact -> get_contact($verify_code);
		$html["relationships"] = $this -> relationship -> get_relationship($verify_code);

		$this -> load -> model('users_model');
		$html["users"] = $this -> users_model -> get_all_users();

		$login_user_session = $this -> session -> all_userdata();

		//判斷是否是ol秘書！
		//如果是！他寫入的～可以送給劉小傳

		$html["send_to_admin"] = false;
		$this -> load -> model("role_model");

		$purview = $this -> role_model -> get_info($login_user_session["role"]);

		if ($purview -> role_name == "秘書") {
			$html["send_to_admin"] = true;
		}
		$html["circle"] = $this -> circle_model -> get_all();
		//echo $_GET["type"];
		$html["sshow"] = '';
		if (!empty($_GET["type"])) {
			//echo 'asdasdqweqwecompany';
			$html["sshow"] = $_GET["type"];
		} else {
			$html["sshow"] = "";
		}

		$this -> load -> view('demand/demand_data_edit', $html);
	}

	public function data_edit_submit() {


		$this -> load -> model('demand_model');
		$this -> load -> model('client_model');
		
		$id = $this -> input -> post('demand_id');
		
		
		$data['type_rent'] = $this -> input -> post('type_rent');
		$data['type_sell'] = $this -> input -> post('type_sell');
		$data['type_consignment'] = $this -> input -> post('type_consignment');
		$data['object_usage'] = $this -> input -> post('object_usage');
		//$data['mobile'] = $this->input->post('mobile');
		//$data['home_phone'] = $this->input->post('home_phone');
		//$data['company_phone'] = $this->input->post('company_phone');
		//$data['home_address'] = $this->input->post('home_address');
		//$data['building_name'] = $this->input->post('building_name');
		/*
		 if ($this->input->post('building_name') != "") {

		 $re_id = $this->check_buidling_name("value", $data['building_name']);
		 $data['building_name'] = $re_id;
		 }
		 */
		$case_type = "";
		$case_type_flag = true;

		if ($this -> input -> post('object_case_type')) {

			$ar_object_case_types = $this -> input -> post('object_case_type');

			foreach ($ar_object_case_types as $num => $value) {
				//echo $value;

				if ($case_type_flag) {

					$case_type_flag = false;
					$case_type .= $value;
				} else {
					$case_type .= "," . $value;
				}
			}
		}

		//echo $case_type;
		$data['object_case_type'] = $case_type;

		//$data['company_address'] = $this->input->post('company_address');
		//$data['company_name'] = $this->input->post('company_name');
		//$data['email'] = $this->input->post('email');
		//$data['contact'] = $this->input->post('contact');
		//$data['contact_phone'] = $this->input->post('contact_phone');
		$data['building_area1'] = $this -> input -> post('building_area1');
		$data['building_area2'] = $this -> input -> post('building_area2');
		$data['parking_number'] = $this -> input -> post('parking_number');
		$data['acceptable_mechanical'] = $this -> input -> post('acceptable_mechanical');
		$data['indoor_area1'] = $this -> input -> post('indoor_area1');
		$data['indoor_area2'] = $this -> input -> post('indoor_area2');
		$data['land_area1'] = $this -> input -> post('land_area1');
		$data['land_area2'] = $this -> input -> post('land_area2');
		$data['key_areas1'] = $this -> input -> post('key_areas1');
		$data['key_areas2'] = $this -> input -> post('key_areas2');
		$data['specify_building1'] = $this -> input -> post('specify_building1');
		$data['specify_building2'] = $this -> input -> post('specify_building2');
		$data['sections1'] = $this -> input -> post('sections1');
		$data['sections2'] = $this -> input -> post('sections2');
		$data['object_name1'] = $this -> input -> post('object_name1');
		$data['object_name2'] = $this -> input -> post('object_name2');
		$data['mrt_line1'] = $this -> input -> post('mrt_line1');
		$data['mrt_start1'] = $this -> input -> post('mrt_start1');
		$data['mrt_until1'] = $this -> input -> post('mrt_until1');
		$data['mrt_line2'] = $this -> input -> post('mrt_line2');
		$data['mrt_start2'] = $this -> input -> post('mrt_start2');
		$data['mrt_until2'] = $this -> input -> post('mrt_until2');
		$data['range_east'] = $this -> input -> post('range_east');
		$data['range_north'] = $this -> input -> post('range_north');
		$data['range_west'] = $this -> input -> post('range_west');
		$data['range_south'] = $this -> input -> post('range_south');
		$data['park1'] = $this -> input -> post('park1');
		$data['park2'] = $this -> input -> post('park2');
		$data['school1'] = $this -> input -> post('school1');
		$data['school2'] = $this -> input -> post('school2');
		$data['circle1'] = $this -> input -> post('circle1');
		$data['circle2'] = $this -> input -> post('circle2');
		$data['administrative_area1'] = $this -> input -> post('administrative_area1');
		$data['administrative_area2'] = $this -> input -> post('administrative_area2');
		$data['total_budget1'] = $this -> input -> post('total_budget1');
		$data['total_budget2'] = $this -> input -> post('total_budget2');
		$data['budget_per_ping1'] = $this -> input -> post('budget_per_ping1');
		$data['budget_per_ping2'] = $this -> input -> post('budget_per_ping2');
		$data['reported_rate1'] = $this -> input -> post('reported_rate1');
		$data['reported_rate2'] = $this -> input -> post('reported_rate2');
		$data['leasing_business_purposes'] = $this -> input -> post('leasing_business_purposes');
		$data['year_of_construction'] = $this -> input -> post('year_of_construction');
		//$data['object_case_type'] = join("/", $this->input->post('object_case_type'));

		$data['windows'] = $this -> input -> post('windows');
		$data['facing_the_household'] = $this -> input -> post('facing_the_household');
		$data['building_facing'] = $this -> input -> post('building_facing');
		$data['designated_floors1'] = $this -> input -> post('designated_floors1');
		$data['designated_floors2'] = $this -> input -> post('designated_floors2');
		$data['floors'] = $this -> input -> post('floors');
		$data['object_styles'] = $this -> input -> post('object_styles');
		$data['public_facilities'] = $this -> input -> post('public_facilities');
		$data['accessories_with'] = $this -> input -> post('accessories_with');
		$data['management'] = $this -> input -> post('management');
		$data['number_of_rooms1'] = $this -> input -> post('number_of_rooms1');
		$data['number_of_rooms2'] = $this -> input -> post('number_of_rooms2');
		$data['number_of_hall1'] = $this -> input -> post('number_of_hall1');
		$data['number_of_hall2'] = $this -> input -> post('number_of_hall2');
		$data['number_of_toilet1'] = $this -> input -> post('number_of_toilet1');
		$data['number_of_toilet2'] = $this -> input -> post('number_of_toilet2');
		$data['number_of_suite1'] = $this -> input -> post('number_of_suite1');
		$data['number_of_suite2'] = $this -> input -> post('number_of_suite2');
		$data['partition_the'] = $this -> input -> post('partition_the');
		$data['floor_height'] = $this -> input -> post('floor_height');
		$data['customer_source'] = $this -> input -> post('customer_source');
		$data['required_level'] = $this -> input -> post('required_level');
		$data['remark'] = $this -> input -> post('remark');
		$data['currency'] = $this -> input -> post('currency');
		$data['area_unit'] = $this -> input -> post('area_unit');
		$data['remark'] = $this -> input -> post('remark');
		$data['status'] = $this -> input -> post('status');
		$data['invalid_description'] = $this -> input -> post('invalid_description');
		$data['front_man'] = $this -> input -> post('front_man');
		$data['head'] = $this -> input -> post('head');
		$data['original_broker'] = $this -> input -> post('original_broker');
		$data['broker'] = $this -> input -> post('broker');

		$data['client_id'] = $this -> input -> post('client_id');
		$data['client_contact_id'] = $this -> input -> post('client_contact_id');
		$data['ref_client_verify_code'] = $this -> input -> post('ref_client_verify_code');
		
		$data['sercet_level'] = $this -> input -> post('sercet_level');
		$data['is_public'] = $this -> input -> post('is_public');
		$data['important'] = $this -> input -> post('important');
		$data['is_see'] = $this -> input -> post('is_see');
		$data['mtime'] = strtotime(date("Y-m-d h:i:s"));
		
		$this -> demand_model ->update($id,$data);	
			
		//$return_result = $this -> demand_model -> update_demand($data, $id);
		//修改要存入歷史資料／先拿掉／因為做爽的！＠＠！
		//$this -> demand_model -> insert_demand_history($data, $id);
		
		$pair_submit = $this -> input -> post('pair_submit');
		$client = $this -> input -> post('client');

		//print_r($_POST);
		//echo $_POST["client_contact_id"];

		$_contact = $_POST["contact"];

		$contact_id = $_POST["client_contact_id"];
		$this -> load -> model("client/relationship", 'relationship');
		//更新聯絡人 ， 客戶
		$this -> client_model -> update_client_two($client, $data['client_id']);
		$this -> client_model -> update_contact($_contact, $contact_id);
		//print_r($data);
		//die();
		/*
		if ($return_result) {

			if ($pair_submit) {
				redirect("/pair/demand");
			} else {
				redirect("/demand/data_query_results");
			}

		} else {

			if ($pair_submit) {
	*/
				redirect("/demand/person_query");
	//		} else {
		//		redirect("/demand/data_query_results");
	////		}
		//}

	}

	public function info($id = null) {
		$html = $this -> init("需求資料", "jquery", "form");
		$this -> load -> model('object_usage_model');
		$html["object_usage"] = $this -> object_usage_model -> get_object_usage();
		$this -> load -> model('mrt_line_model');
		$html["mrt_line"] = $this -> mrt_line_model -> get_mrt_line();
		$this -> load -> model('windows_model');
		$html["windows"] = $this -> windows_model -> get_windows();
		$this -> load -> model('base_is_located_model');
		$html["base_is_located"] = $this -> base_is_located_model -> get_base_is_located();
		$this -> load -> model('object_styles_model');
		$html["object_styles"] = $this -> object_styles_model -> get_object_styles();
		$this -> load -> model('public_facilities_model');
		$html["public_facilities"] = $this -> public_facilities_model -> get_public_facilities();
		$this -> load -> model('affiliated_with_model');
		$html["affiliated_with"] = $this -> affiliated_with_model -> get_affiliated_with();
		$this -> load -> model('management_model');
		$html["management"] = $this -> management_model -> get_management();
		$this -> load -> model('land_use_zoning_model');
		$html["land_use_zoning"] = $this -> land_use_zoning_model -> get_land_use_zoning();
		$this -> load -> model('customer_source_model');
		$html["customer_source"] = $this -> customer_source_model -> get_customer_source();
		$this -> load -> model('required_level_model');
		$html["required_level"] = $this -> required_level_model -> get_required_level();
		$this -> load -> model('currency_model');
		$html["currency"] = $this -> currency_model -> get_currency();
		$this -> load -> model('area_unit_model');
		$html["area_unit"] = $this -> area_unit_model -> get_area_unit();
		$this -> load -> model('demand_model');
		$this -> load -> model("demand_m");
		$demand_res = $this -> demand_m -> get_demand_info($id);
		$html["demand"] = $demand_res -> row();

		$this -> load -> model('mrt_model');
		$html["mrt"] = $this -> mrt_model -> get_mrt();

		$this -> load -> view('demand_info', $html);
	}

	public function data_delete($id = "") {
		$this -> load -> model('demand_model');
		if ($id != null) {
			$data["id"] = $id;
			$return_result = $this -> demand_model -> del_demand($data);
			//echo $this->db->last_query();die();
			if ($return_result) {
				redirect("/demand/data_query_results");
			} else {
				echo "刪除失敗";
			}
		} else {
			echo "刪除失敗";
		}
	}

	public function excel($id = null) {

		@session_start();
		$html = $this -> init("需求資料", "jquery", "form");
		$this -> load -> model('object_usage_model');
		$html["object_usage"] = $this -> object_usage_model -> get_object_usage();
		$this -> load -> model('mrt_line_model');
		$html["mrt_line"] = $this -> mrt_line_model -> get_mrt_line();
		$this -> load -> model('windows_model');
		$html["windows"] = $this -> windows_model -> get_windows();
		$this -> load -> model('base_is_located_model');
		$html["base_is_located"] = $this -> base_is_located_model -> get_base_is_located();
		$this -> load -> model('object_styles_model');
		$html["object_styles"] = $this -> object_styles_model -> get_object_styles();
		$this -> load -> model('public_facilities_model');
		$html["public_facilities"] = $this -> public_facilities_model -> get_public_facilities();
		$this -> load -> model('affiliated_with_model');
		$html["affiliated_with"] = $this -> affiliated_with_model -> get_affiliated_with();
		$this -> load -> model('management_model');
		$html["management"] = $this -> management_model -> get_management();
		$this -> load -> model('land_use_zoning_model');
		$html["land_use_zoning"] = $this -> land_use_zoning_model -> get_land_use_zoning();
		$this -> load -> model('customer_source_model');
		$html["customer_source"] = $this -> customer_source_model -> get_customer_source();
		$this -> load -> model('required_level_model');
		$html["required_level"] = $this -> required_level_model -> get_required_level();
		$this -> load -> model('currency_model');
		$html["currency"] = $this -> currency_model -> get_currency();
		$this -> load -> model('area_unit_model');
		$html["area_unit"] = $this -> area_unit_model -> get_area_unit();
		$this -> load -> model('demand_model');

		$this -> load -> model('users_model');
		$html["users"] = $this -> users_model -> get_all_users();
		//print_r($_POST);
		$post_ids = false;

		$demand_ids = array();
		if (!empty($_SESSION["demand_page"])) {
			foreach ($_SESSION["demand_page"] as $id => $flag) {
				if ($flag == 1) {
					//$post_ids = $_SESSION["client_page"];
					$demand_ids[] = $id;
				}
			}

			$post_ids = $demand_ids;
			//$_SESSION["client_page"];
		}

		$res = $this -> demand_model -> get_demand_do_excel($post_ids);

		$html["result"] = $res -> result();
		$this -> load -> model('mrt_model');

		$html["mrt"] = $this -> mrt_model -> get_mrt();
		if ($_SESSION["demand_page"]) {
			session_unset($_SESSION["demand_page"]);
			//session_destroy();
		}
		//$this->load->view('demand_excel', $html);
		$this -> load -> view('excel/vdemand', $html);
	}

	public function do_all_excel($id = null) {

		$_type;
		if ($_GET["type"]) {
			$_type = $_GET["type"];
		} else {
			$_type = 'person';
		}

		$html = $this -> init("需求資料", "jquery", "form");

		$this -> load -> model('building_name_model');

		$html["building_names"] = $this -> building_name_model -> get_all();

		$this -> load -> model('object_usage_model');
		$html["object_usage"] = $this -> object_usage_model -> get_object_usage();
		$this -> load -> model('mrt_line_model');
		$html["mrt_line"] = $this -> mrt_line_model -> get_mrt_line();
		$this -> load -> model('windows_model');
		$html["windows"] = $this -> windows_model -> get_windows();
		$this -> load -> model('base_is_located_model');
		$html["base_is_located"] = $this -> base_is_located_model -> get_base_is_located();
		$this -> load -> model('object_styles_model');
		$html["object_styles"] = $this -> object_styles_model -> get_object_styles();
		$this -> load -> model('public_facilities_model');
		$html["public_facilities"] = $this -> public_facilities_model -> get_public_facilities();
		$this -> load -> model('affiliated_with_model');
		$html["affiliated_with"] = $this -> affiliated_with_model -> get_affiliated_with();
		$this -> load -> model('management_model');
		$html["management"] = $this -> management_model -> get_management();
		$this -> load -> model('land_use_zoning_model');
		$html["land_use_zoning"] = $this -> land_use_zoning_model -> get_land_use_zoning();
		$this -> load -> model('customer_source_model');
		$html["customer_source"] = $this -> customer_source_model -> get_customer_source();
		$this -> load -> model('required_level_model');
		$html["required_level"] = $this -> required_level_model -> get_required_level();
		$this -> load -> model('currency_model');
		$html["currency"] = $this -> currency_model -> get_currency();
		$this -> load -> model('area_unit_model');
		$html["area_unit"] = $this -> area_unit_model -> get_area_unit();
		$this -> load -> model('demand_model');
		$this -> load -> model('mrt_model');
		$html["mrt"] = $this -> mrt_model -> get_mrt();
		$this -> load -> model('users_model');
		$html["users"] = $this -> users_model -> get_all_users();

		$res;

		if ($_type == 'person') {

			//個人
			$res = $this -> demand_model -> get_demand_person_excel();
			$html["result"] = $res -> result();
			$this -> load -> view('excel/vdemand', $html);

		} elseif ($_type == 'company') {

			//公司

			$select_fields = $this -> clean_field(array(''));

			$res = $this -> demand_model -> get_demand_company_excel($select_fields);
			$html["selsect_fields"] = $select_fields;
			$html["result"] = $res -> result();

			$this -> load -> view('excel/vdemand_company', $html);

		}

	}

	//測試
	public function test_fget() {

		$q = $this -> clean_field(array('name', 'mobile', 'home_phone', 'company_phone', 'home_address', 'building_name', 'company_address', 'company_name', 'email', 'contact', 'contact_phone '));
		//print_r($q);
	}

	//找欄位名稱～不要那幾個
	public function clean_field($ary) {

		$_fields = $this -> get_demand_field();
		$_fields_array = array();

		foreach ($_fields as $key => $field) {

			$i_flag = true;

			foreach ($ary as $_k => $_not_name) {

				if ($_not_name == $field) {
					//echo $key.'/';

					$_fields[$key] = '';

				}
			}

		}

		foreach ($_fields as $key => $field) {
			if (!empty($field))
				$_fields_array[] = $field;
			//array();
		}

		//print_r($_fields);

		return $_fields_array;

	}

	public function del_demand_id() {

		$this -> load -> model('demand_model');
		@session_start();

		foreach ($_SESSION["demand_page"] as $val => $one) {
			if (!empty($val)) {
				$this -> demand_model -> del_demand_id($val);
			}
		}

		if (!empty($_SESSION["demand_page"])) {
			session_unset($_SESSION["object_page"]);
		}
	}

	public function show_demand_id() {

		@session_start();
		print_r($_SESSION);
	}

	public function save_demand_id() {
		@session_start();

		//print_r($_POST);

		if ($_POST["is_del"] == 0) {
			$_str = explode(',', $_POST["id"]);

			//$_SESSION["object_page"][$_POST["id"]] = 1;
			foreach ($_str as $num => $val) {
				$_SESSION["demand_page"][$val] = 1;
			}
		} elseif ($_POST["is_del"] == 1) {

			$_SESSION["demand_page"][$_POST["id"]] = 0;
		}
	}

	public function show_circle() {

		$this -> load -> view('just_show/circle');

	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
